package com.yc.web.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oyj.dao.DBHelper;
@WebServlet("/goods")
public class GoodeServlet extends BasicServlet {
	private DBHelper db = new DBHelper();
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/plain;charset=utf-8");
		
		//得到op
		String op = req.getParameter("op");
		
		//开始判断
		if( "findFirst".equals(op) ){
			findFirst(req,resp);
		}else if("finds".equals(op)){
			finds(req,resp);
		}else if( "findByGno".equals(op)){
			findByGno(req,resp);
		}
	}
	
	private void findByGno(HttpServletRequest req, HttpServletResponse resp) {
		int gno = Integer.parseInt(req.getParameter("gno"));
		String sql = "select * from goodsinfo where gno = ?";
		this.send(resp, db.findAll(sql, gno));
		
		
	}

	//查找数据
	private void finds(HttpServletRequest req, HttpServletResponse resp) {
		int page = Integer.parseInt(req.getParameter("page"));
		int row = Integer.parseInt(req.getParameter("row"));
		
		String sql = "select * from goodsinfo limit ?,?";
		List<Map<String, Object>> list = db.findAll(sql, (page - 1) * row,row);
		this.send(resp, list);
	}

	//代表是第一次进来，也就是首页第一次发送的请求，那么，这一次是比较特殊的
	private void findFirst(HttpServletRequest req, HttpServletResponse resp) {
		int page = Integer.parseInt(req.getParameter("page"));
		int row = Integer.parseInt(req.getParameter("row"));
		
		String gname = req.getParameter("gname");
		String tno = req.getParameter("tno");
		String sprice = req.getParameter("sprice");
		String eprice = req.getParameter("eprice");
		
		//先查询总共有多少条数据
		//	  根据条件，查询总数据
		int total = findTotal(gname,tno,sprice,eprice);
		
		//查询当前页的数据
		List<Map<String, Object>> list = findGoodsByItem(gname,tno,sprice,eprice,row,page);
		
		//将总条数，追加到list的最后面
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", total);
		
		list.add(map);
		
		//将这个数据，返回给前端页面
		this.send(resp, list);
	}

	private List<Map<String, Object>> findGoodsByItem(String gname, String tno, String sprice, String eprice, int row,
			int page) {
		String sql = "select * from goodsinfo where status != 0  ";
		//开始拼接条件
		if( gname != null && !"".equals(gname) ){
			sql += "  and gname like '%"+gname+"%' ";
		}
		if( tno != null && !"".equals(tno) ){
			int x = Integer.parseInt(tno);
			sql += "  and tno = "+x;
		}
		if( sprice != null && !"".equals(sprice) ){
			double x = Double.parseDouble(sprice);
			sql += "  and price >= "+x;
		}
		if( eprice != null && !"".equals(eprice) ){
			double x = Double.parseDouble(eprice);
			sql += "  and price <= "+x;
		}
		sql += "limit ?,?";
		return db.findAll(sql, (page-1)*row,row);
	}

	private int findTotal(String gname, String tno, String sprice, String eprice) {
		String sql = "select * from goodsinfo where status != 0  ";
		if( gname != null && !"".equals(gname) ){
			sql += "  and gname like '%"+gname+"%' ";
		}
		if( tno != null && !"".equals(tno) ){
			int x = Integer.parseInt(tno);
			sql += "  and tno = "+x;
		}
		if( sprice != null && !"".equals(sprice) ){
			double x = Double.parseDouble(sprice);
			sql += "  and price >= "+x;
		}
		if( eprice != null && !"".equals(eprice) ){
			double x = Double.parseDouble(eprice);
			sql += "  and price <= "+x;
		}
		return db.findAll(sql).size();
	}

}
